Const MENU_NEW=101
Const MENU_OPEN=102
Const MENU_SAVE=103
Const MENU_CLOSE=104
Const MENU_EXIT=105

Const MENU_EDTRANS = 201
Const MENU_EDROT = 202

Const MENU_MAPRPS = 301

Const MENU_OMAN = 501
Const MENU_NMAN = 502

Type CedUI Extends CUI

	Field FPSTimer:TTimer
	Field grid:TMesh
	Field gridt:TTexture
	Field cam:TCamera
	Field light:TLight
	Field OldMX:Int
	Field OldMy:Int
	Field MDownA:Byte
	Field MDownB:Byte
	Field Co:CGObjs
	Field No:CGNodes
	Field Mp:MapProps
	Field nexty:Int = 5
	Field nextx:Int = 5
	Field fitbox:TEntity
	Field fittex:TTexture
	Field transg:TransformGizmo
	Field rotg:RotationGizmo
	Field Picked:TEntity
	Field trnmode:Int = 0
	Method Init(p:CUI=Null)
	
		Local gads:TGadget[10]
		window = CreateWindow("GenEd3D v-"+GENED3D_VERSION, 0, 0, ClientWidth(Desktop()), ClientHeight(Desktop()),Null,WINDOW_TITLEBAR|WINDOW_MENU|WINDOW_RESIZABLE|WINDOW_STATUS|WINDOW_CENTER )
		gads[0] = CreateCanvas(5,5,(ClientWidth(window)/3)*2,ClientHeight(window),window,0)
		nextx = nextx+5+GadgetWidth(gads[0])
		
		gads[1] = CreateLabel("Scene Objects",nextx,nexty,(ClientWidth(window)/3)-10,20,window)
		nexty=nexty+5+GadgetHeight(gads[1])
		gads[2] = CreateListBox(nextx,nexty,(ClientWidth(window)/3)-10,150,window)
		

		Local filemenu:TGadget=CreateMenu("&File",0,WindowMenu(window))
		CreateMenu"&New",MENU_NEW,filemenu,KEY_N,MODIFIER_COMMAND
		CreateMenu"&Open",MENU_OPEN,filemenu,KEY_O,MODIFIER_COMMAND
		CreateMenu"&Close",MENU_CLOSE,filemenu,KEY_W,MODIFIER_COMMAND
		CreateMenu"",0,filemenu
		CreateMenu"&Save",MENU_SAVE,filemenu,KEY_S,MODIFIER_COMMAND
		CreateMenu"",0,filemenu
		CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND

		filemenu=CreateMenu("&Edit",0,WindowMenu(window))
		CreateMenu"Postion",MENU_EDTRANS,filemenu
		CreateMenu"Rotate",MENU_EDROT,filemenu
		
		filemenu=CreateMenu("&Map",0,WindowMenu(window))
		CreateMenu"Map Properties",MENU_MAPRPS,filemenu

		filemenu=CreateMenu("&Window",0,WindowMenu(window))
		CreateMenu"Object Manager",MENU_OMAN,filemenu
		CreateMenu"Node Manager",MENU_NMAN,filemenu


		UpdateWindowMenu(window)

		
		TGlobal.width=ClientWidth(window)
		TGlobal.height=ClientHeight(window)
		
		TGlobal.depth=16
		TGlobal.mode=0
		TGlobal.rate=60
		
		SetGadgetLayout( gads[0],EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED )
		SetGraphics CanvasGraphics(gads[0])
		TGlobal.GraphicsInit()
		
		FPSTimer = CreateTimer( 60 )
		
		gadgets = gads
		Init3D()
		Co = New CGObjs
		Co.Init()
		No = New CGNodes
		No.Init()
		Mp = New MapProps
		Mp.Init()
				
		Super.Init(p)
	EndMethod

	Method Init3D()

		cam=CreateCamera()
		PositionEntity cam,0,5,-10
		light=CreateLight(1)
		grid=LoadMesh("editores/grid.b3d")
		ScaleEntity(grid,1000,1,1000)
		EntityColor grid,255,255,255
		gridt=LoadTexture("editores/grid.png",3)
		ScaleTexture gridt,0.001,0.001
		EntityTexture grid,gridt
		EntityFX(grid,1)	
		fitbox = CreateCube()
		fittex=LoadTexture("editores/fitbox.png",3)
		EntityTexture fitbox,fittex
		EntityFX(fitbox,1)
		FlipMesh(TMesh(fitbox))
		transg = New TransformGizmo
		transg.Create()
		rotg = New RotationGizmo
		rotg.Create()
		HideEntity(rotg.piv)
	End Method

	Method RefreshSceneList()
		ClearGadgetItems(gadgets[2])
		For Local mo:MapObject = EachIn MapObjects
		
			AddGadgetItem(gadgets[2],mo.name,0,-1,"",mo)
		
		Next
	
	EndMethod

	Method Update(EID:Int)

		Select EID
			Case EVENT_GADGETSELECT
				Select EventSource()
				
						Case gadgets[2]
						DebugLog "Selected Scene list"
						If SelectedGadgetItem(gadgets[2]) <> -1 Then
							Local mo:MapObject = MapObject(GadgetItemExtra(gadgets[2],SelectedGadgetItem(gadgets[2])))
							If mo <> Null Then
								DebugLog "Got Entity"
								Local msh:TMesh = TMesh(mo.Mesh)
								'check if camera
								For Local mm:MapObject = EachIn MapObjects
									Local c:TCamera = TCamera(mm.Mesh)
									If c <> Null Then
										HideEntity(c)
									EndIf
								Next
								Local c:TCamera = TCamera(mo.Mesh)
								If c <> Null Then
									ShowEntity(mo.Mesh)
								EndIf
								If msh <> Null Then
									DebugLog "Was mesh"
									FitMesh(TMesh(fitbox),-(MeshWidth(msh)/2),-(MeshHeight(msh)/2),-(MeshDepth(msh)/2),MeshWidth(msh),MeshHeight(msh),MeshDepth(msh))
								Else
									FitMesh(TMesh(fitbox),-0.5,-0.5,-0.5,1,1,1)									
								EndIf
								
								PositionEntity(transg.piv,EntityX(mo.Mesh,True),EntityY(mo.Mesh,True),EntityZ(mo.Mesh,True),True)
								PositionEntity(rotg.piv,EntityX(mo.Mesh,True),EntityY(mo.Mesh,True),EntityZ(mo.Mesh,True),True)
								PositionEntity(fitbox,EntityX(mo.Mesh,True),EntityY(mo.Mesh,True),EntityZ(mo.Mesh,True),True)

								RotateEntity(transg.piv,EntityPitch(mo.Mesh,True),EntityYaw(mo.Mesh,True),EntityRoll(mo.Mesh,True),True)
								RotateEntity(rotg.piv,EntityPitch(mo.Mesh,True),EntityYaw(mo.Mesh,True),EntityRoll(mo.Mesh,True),True)
								RotateEntity(fitbox,EntityPitch(mo.Mesh,True),EntityYaw(mo.Mesh,True),EntityRoll(mo.Mesh,True),True)
								
							EndIf
						EndIf
			
				EndSelect
			Case EVENT_GADGETACTION
				Select EventSource()
				
				
				EndSelect
		
			Case EVENT_MENUACTION
				Select EventData()
				
					Case MENU_NEW
						NewMap()
					
					Case MENU_OPEN
						NewMap()
						Local filter:String="Map Files:map"
						Local filename:String=RequestFile( "Load Map",filter,False,WorkingDir+"\Maps\" )
						If filename <> "" Then
							LoadMap(filename$)
						EndIf						
						
					Case MENU_EDTRANS
						trnmode = 0
						ShowEntity(transg.piv)
						HideEntity(rotg.piv)

					Case MENU_EDROT
						trnmode = 1
						ShowEntity(rotg.piv)
						HideEntity(transg.piv)
				
					Case MENU_OMAN
						ShowGadget(Co.window)
						ActivateGadget( Co.window )
						
					Case MENU_NMAN
						ShowGadget(No.window)
						ActivateGadget( No.window )
						
					Case MENU_SAVE
						Local filter:String="Map Files:map"
						Local filename:String=RequestFile( "Save Map as",filter,True,WorkingDir+"\Maps\Untitled" )
						If filename <> "" Then
							CURRENTMAP.Save(filename$)
						EndIf
						
				EndSelect
			Case EVENT_MOUSEDOWN
				If EventData() = 2 Then MDownA = True
				If EventData() = 1 Then MDownB = True
			Case EVENT_MOUSEUP	
				If EventData() = 2 Then MDownA = False
				If EventData() = 1 Then MDownB = False
			Case EVENT_MOUSEWHEEL
					MoveEntity(cam,0,0,EventData())
			Case EVENT_MOUSEMOVE
				'DebugLog "Mouse move X: "+EventX()+" Y: "+EventY()
				If MDownA = True Then
					TurnEntity(cam,-(EventY()-OldMY),(EventX()-OldMX),0)
				EndIf
				If MDownB = True Then
					If Picked <> Null And SelectedGadgetItem(gadgets[2]) <> -1 Then
						Local mo:MapObject = MapObject(GadgetItemExtra(gadgets[2],SelectedGadgetItem(gadgets[2]) ))
						If mo <> Null Then
						'check gizmo
							If Picked = rotg.xaxis Then
								TurnEntity(transg.piv,EventX()-OldMX,0,0)
								TurnEntity(rotg.piv,EventX()-OldMX,0,0)
								TurnEntity(mo.Mesh,EventX()-OldMX,0,0)	
								TurnEntity(fitbox,EventX()-OldMX,0,0)
								mo.Node.Attribute("rot").value = ""+EntityPitch(mo.Mesh,True)+","+EntityYaw(mo.Mesh,True)+","+EntityRoll(mo.Mesh,True)						
							EndIf
							If Picked = rotg.yaxis Then
								TurnEntity(transg.piv,0,EventX()-OldMX,0)
								TurnEntity(rotg.piv,0,EventX()-OldMX,0)
								TurnEntity(mo.Mesh,0,EventX()-OldMX,0)	
								TurnEntity(fitbox,0,EventX()-OldMX,0)
								mo.Node.Attribute("rot").value = ""+EntityPitch(mo.Mesh,True)+","+EntityYaw(mo.Mesh,True)+","+EntityRoll(mo.Mesh,True)						
							EndIf
							If Picked = rotg.zaxis Then
								TurnEntity(transg.piv,0,0,EventX()-OldMX)
								TurnEntity(rotg.piv,0,0,EventX()-OldMX)
								TurnEntity(mo.Mesh,0,0,EventX()-OldMX)	
								TurnEntity(fitbox,0,0,EventX()-OldMX)
								mo.Node.Attribute("rot").value = ""+EntityPitch(mo.Mesh,True)+","+EntityYaw(mo.Mesh,True)+","+EntityRoll(mo.Mesh,True)						
							EndIf
						
							If Picked = transg.xaxis Then
								MoveEntity(transg.piv,EventX()-OldMX,0,0)
								MoveEntity(rotg.piv,EventX()-OldMX,0,0)
								MoveEntity(mo.Mesh,EventX()-OldMX,0,0)	
								MoveEntity(fitbox,EventX()-OldMX,0,0)
								mo.Node.Attribute("pos").value = ""+EntityX(mo.Mesh,True)+","+EntityY(mo.Mesh,True)+","+EntityZ(mo.Mesh,True)						
							EndIf
							If Picked = transg.yaxis Then
								MoveEntity(transg.piv,0,EventX()-OldMX,0)
								MoveEntity(rotg.piv,0,EventX()-OldMX,0)
								MoveEntity(mo.Mesh,0,EventX()-OldMX,0)	
								MoveEntity(fitbox,0,EventX()-OldMX,0)
								mo.Node.Attribute("pos").value = ""+EntityX(mo.Mesh,True)+","+EntityY(mo.Mesh,True)+","+EntityZ(mo.Mesh,True)	
							EndIf
							If Picked = transg.zaxis Then
								MoveEntity(transg.piv,0,0,EventX()-OldMX)
								MoveEntity(rotg.piv,0,0,EventX()-OldMX)
								MoveEntity(mo.Mesh,0,0,EventX()-OldMX)	
								MoveEntity(fitbox,0,0,EventX()-OldMX)
								mo.Node.Attribute("pos").value = ""+EntityX(mo.Mesh,True)+","+EntityY(mo.Mesh,True)+","+EntityZ(mo.Mesh,True)	
							EndIf
							
							
							
						EndIf
					EndIf
				
				EndIf
				OldMX = EventX()
				OldMY = EventY()
			Case EVENT_TIMERTICK
				RedrawGadget gadgets[0]
				
			Case EVENT_GADGETPAINT
				SetGraphics CanvasGraphics(gadgets[0])
				EntityColor(fitbox,Int(Sin(MilliSecs())*255),255,Int(Sin(MilliSecs())*255))
				If MDownB = False Then
					Picked = CameraPick ( cam,OldMX,OldMY)
				EndIf	
					If Picked <> Null Then
					
						'check gizmo
						If Picked = transg.xaxis Then
							EntityColor(transg.xaxis,255,255,0)
						Else
							EntityColor(transg.xaxis,255,0,0)
						EndIf
						If Picked = transg.yaxis Then
							EntityColor(transg.yaxis,255,255,0)
						Else
							EntityColor(transg.yaxis,0,255,0)
						EndIf
						If Picked = transg.zaxis Then
							EntityColor(transg.zaxis,255,255,0)
						Else
							EntityColor(transg.zaxis,0,0,255)
						EndIf

						If Picked = rotg.xaxis Then
							EntityColor(rotg.xaxis,255,255,0)
						Else
							EntityColor(rotg.xaxis,255,0,0)
						EndIf
						If Picked = rotg.yaxis Then
							EntityColor(rotg.yaxis,255,255,0)
						Else
							EntityColor(rotg.yaxis,0,255,0)
						EndIf
						If Picked = rotg.zaxis Then
							EntityColor(rotg.zaxis,255,255,0)
						Else
							EntityColor(rotg.zaxis,0,0,255)
						EndIf


					
					EndIf
				
				RenderWorld
				Flip


			Case EVENT_WINDOWCLOSE
				Select EventSource()


					Case window
						End		

				EndSelect
		EndSelect
		Co.Update(EID)
		No.Update(EID)
		Mp.Update(EID)
	EndMethod
	
	Method LoadMap(file:String)
	
		CURRENTMAP = New xmlDocument
		CURRENTMAP.Load(file)
		If CURRENTMAP.Root().HasChildren() Then
			Local cnode:xmlNode = CURRENTMAP.Root().FirstChild()
			While cnode <> Null 
				Select Lower(cnode.Name)
					Case "gameobject"
						Local mo:MapObject = New MapObject
						mo.Node = cnode
						mo.name = cnode.Attribute("name").Value
						ListAddLast(MapObjects,mo)
						
						Select Lower(cnode.Attribute("type").Value)
						
							Case "mesh"
								mo.Mesh = LoadMesh(WorkingDir+"\models\"+cnode.Attribute("filename").value)
								FlipMesh(TMesh(mo.Mesh))
							
							Case "general"
								mo.Mesh = CreateCube()
							
							Case "omnilight"
								mo.Mesh  = CreateLight(1)
								
							Case "pointlight"
								mo.Mesh  = CreateLight(2)
								
							Case "spotlight"
								mo.Mesh  = CreateLight(3)
								
							Case "sound"
								mo.Mesh  = CreatePivot()
							
							Case "camera"
								mo.Mesh  = CreateCamera()
								CameraViewport TCamera(mo.Mesh),10,10,320,240
								HideEntity(mo.Mesh)
										
						EndSelect
						If cnode.Attribute("pos").Value<>""Then
							Local pos:String[] = cnode.Attribute("pos").Value.Split(",")
							PositionEntity(mo.Mesh,Float(pos[0]),Float(pos[1]),Float(pos[2]))
						EndIf
						If cnode.Attribute("rot").Value<>""Then
							Local pos:String[] = cnode.Attribute("rot").Value.Split(",")
							RotateEntity(mo.Mesh,Float(pos[0]),Float(pos[1]),Float(pos[2]))
						EndIf
						
				EndSelect
				cnode = cnode.NextSibling()
			Wend
			
		EndIf		
		RefreshSceneList()
	EndMethod

	Method NewMap()
		Local result:Int=Confirm("Are you sure?")
		If result = 0 Then Return
		For Local mo:MapObject = EachIn MapObjects
		
			FreeEntity(mo.Mesh)
			
		Next
		ClearList(MapObjects)
		RefreshSceneList()
		CURRENTMAP = New xmlDocument
		CURRENTMAP.Root().Name = "Map"
		
	EndMethod

EndType
